<?php
/**
 * @package      OWNServices
 * @subpackage   site
 *
 * @author       Sanawin Tinsuwan
 * @copyright    Copyright (C) 20014 Sanawin Tinsuwan. All rights reserved.
 * @license      http://www.gnu.org/licenses/gpl.html GNU/GPL, see LICENSE.txt
 */

defined('_JEXEC') or die();

// import Joomla controllerform library
jimport('joomla.application.component.controllerform');

/**
 * User controller class
 */

Class OwnservicesControllerUser extends JControllerForm
{
    /**
     * Method to log in a user.
     *
     * @since   1.6
     */
    public function login()
    {
        JSession::checkToken('post') or jexit(JText::_('JInvalid_Token'));

        $app    = JFactory::getApplication();
        $input  = $app->input;
        $method = $input->getMethod();

        // Populate the data array:
        $data = array();

        $data['return']    = base64_decode($app->input->post->get('return', '', 'BASE64'));
        $data['username']  = $input->$method->get('username', '', 'USERNAME');
        $data['password']  = $input->$method->get('password', '', 'RAW');
        $data['secretkey'] = $input->$method->get('secretkey', '', 'RAW');

        // Set the return URL if empty.
        if (empty($data['return']))
        {
            $data['return'] = 'index.php?option=com_users&view=profile';
        }

        // Set the return URL in the user state to allow modification by plugins
        $app->setUserState('users.login.form.return', $data['return']);

        // Get the log in options.
        $options = array();
        $options['remember'] = $this->input->getBool('remember', false);
        $options['return']   = $data['return'];

        // Get the log in credentials.
        $credentials = array();
        $credentials['username']  = $data['username'];
        $credentials['password']  = $data['password'];
        $credentials['secretkey'] = $data['secretkey'];

        // Perform the log in.
        if (true === $app->login($credentials, $options))
        {
            // Success
            if ($options['remember'] == true)
            {
                $app->setUserState('rememberLogin', true);
            }

            $app->setUserState('users.login.form.data', array());
            $app->redirect(JRoute::_($app->getUserState('users.login.form.return'), false));
        }
        else
        {
            // Login failed !
            $data['remember'] = (int) $options['remember'];
            $app->setUserState('users.login.form.data', $data);
            $app->redirect(JRoute::_('index.php?option=com_ownservices&view=login', false));
        }
    }

    /**
     * Method to log out a user.
     *
     * @since   1.6
     */
    public function logout()
    {
        JSession::checkToken('request') or jexit(JText::_('JInvalid_Token'));

        $app = JFactory::getApplication();

        // Perform the log in.
        $error  = $app->logout();
        $input  = $app->input;
        $method = $input->getMethod();

        // Check if the log out succeeded.
        if (!($error instanceof Exception))
        {
            // Get the return url from the request and validate that it is internal.
            $return = $input->$method->get('return', '', 'BASE64');
            $return = base64_decode($return);
            if (!JUri::isInternal($return))
            {
                $return = '';
            }

            // Redirect the user.
            $app->redirect(JRoute::_($return, false));
        }
        else
        {
            $app->redirect(JRoute::_('index.php?option=com_users&view=login', false));
        }
    }
}